<template>
	<view class="background-body">
		<view class="tabs-box">
			<u-tabs :list="list1" @click="tabclick" :current="currTab"></u-tabs>
		</view>
		<at-rece-list :iempty="empty" :resulist="resuList" :type="listType" @pidORuid="getpidORuid"
			@remove="removeShow"></at-rece-list>
		<u-modal :show="showModal" @confirm="removeConfirm" ref="uModal" :asyncClose="true" :closeOnClickOverlay="true"
			showCancelButton @cancel="showModal=false" @close="showModal=false" title='确定要删除这条投递吗'
			content="这将会撤回并且删除此条投递,请谨慎操作"></u-modal>
	</view>
</template>

<script>
	import {
		receResulist,
		deliverResulist,
		deleteDeliver
	} from "@/common/http.api.js"
	import AtUserItem from "@/components/AtUserItem"
	import AtReceList from "@/components/AtReceList"
	export default {
		components: {
			AtUserItem,
			AtReceList
		},
		data() {
			return {
				_id: '',
				currTab: 0,
				timer1: null,
				timer2: null,
				empty: false,
				showModal: false,
				resuList: [],
				scrollHeight: 600,
				listType: '0',
				list1: [{
					type: '0',
					name: '收到投递',
					badge: {
						value: 0,
					}

				}, {
					type: '1',
					name: '我的投递',
				}]
			}
		},

		onLoad() {
			uni.setStorageSync('currTab', '0')
			// this.currTab ? this.currTab : uni.get
		},
		onShow() {
			let temp = uni.getStorageSync('currTab')
			this.currTab = temp == '0' ? 0 : 1
			temp == '0' ? this.getReceResulist() : this.getDeliverResulist()
		},
		methods: {
			tabclick(item) {
				this.listType = item.type
				// 点击tabs切换不同请求
				if (item.type === '0') {
					this.resuList = [] // 优化重置效果.
					uni.setStorageSync('currTab', '0')
					this.getReceResulist()
				} else {
					this.resuList = []
					uni.setStorageSync('currTab', '1')
					this.getDeliverResulist()
				}

			},
			getpidORuid(id, _id) {
				if (this.listType === '1') {
					// 跳转到项目详情
					uni.navigateTo({
						url: `/pages/projDetail/projDetail?id=${id}`
					})
				} else {
					// 跳转到用户简历
					uni.navigateTo({
						url: `/pages/resume/resume?type=1&userId=${id}&_id=${_id}`
					})

				}
			},
			removeShow(id, status) {
				this._id = id
				// if (status === '0') return uni.$showMsg('不能删除未查看的投递！', 'none')
				this.showModal = true
			},
			async removeConfirm() {
				const res = await deleteDeliver({
					_id: this._id
				})
				if (res.code === 0) uni.$showMsg('删除成功!')
				// 刷新列表
				if (this.listType === '0') {
					this.getReceResulist()
				} else {
					this.getDeliverResulist()
				}
				this.showModal = false
			},
			getReceResulist() {
				this.resuList = []
				let userId = this.$store.state.userId
				this.empty = false
				// uni.$loading()
				if (this.timer1) clearTimeout(this.timer1)
				this.timer1 = setTimeout(async () => {
					const res = await receResulist({
						userId
					})
					this.resuList = res.data
					this.first_id = this.resuList._id
					if (this.resuList.length === 0) this.empty = true
					let mark = this.resuList.filter(i => i.status === '0').length
					this.list1[0].badge.value = mark
				}, 250)

			},
			async getDeliverResulist() {
				let userId = this.$store.state.userId
				this.empty = false
				// uni.$loading()
				if (this.timer2) clearTimeout(this.timer2)
				this.timer2 = setTimeout(async () => {
					const res = await deliverResulist({
						userId
					})
					this.resuList = res.data
					if (this.resuList.length === 0) this.empty = true
				}, 250)

			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabs-box {
		background-color: #fff;
		width: 100%;

		/deep/.u-tabs__wrapper__nav__item {
			padding: 0 120rpx;
		}
	}
</style>
